package servlets;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.RemoteException;
import java.util.Formatter;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import srv.MobileOffersProxy;

/**
 * Servlet implementation class MobileServlet
 */
public class MobileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static final String MOBILE_OFFERS_CREATOR_JSP = "/sampleMobileOffersProxy/MobileOffersCreator.jsp";

	private MobileOffersProxy mobileOffersProxy;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MobileServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		if (action == null || action.trim().length() == 0) {
			return;
		}
		if (action.equals("getModels")) {
			actionGetModels(request, response);
		} else if (action.equals("getOffers")) {
			actionGetOffers(request, response);
		} else if (action.equals("createOffer")) {
			 
		} else if (action.equals("saveOffer")) {
			actionSaveOffer(request, response);
		}

	}

	private void actionGetModels(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String brandIdString = request.getParameter("brandId");
		int brandId = Integer.parseInt(brandIdString);

		try {
			String models = getMobileOffersProxy().getModelsByBrand(brandId);

			PrintWriter writer = response.getWriter();
			StringBuffer sb = new StringBuffer();
			sb.append(models);
			writer.println(sb);
			writer.close();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	private void actionGetOffers(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int brandId = Integer.parseInt(request.getParameter("brandId"));
		int modelId = Integer.parseInt(request.getParameter("modelId"));
		int startYear = Integer.parseInt(request.getParameter("startYear"));
		int endYear = Integer.parseInt(request.getParameter("endYear"));
		double startPrice = Double.parseDouble(request
				.getParameter("startPrice"));
		double endPrice = Double.parseDouble(request.getParameter("endPrice"));

		try {
			String offersString = getMobileOffersProxy().getOffersByFilter(
					brandId, modelId, endYear, startYear, endPrice, startPrice);
			PrintWriter writer = response.getWriter();
			StringBuffer sb = new StringBuffer();
			sb.append(offersString);
			writer.println(sb);
			writer.close();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	private void actionSaveOffer(HttpServletRequest request,
			HttpServletResponse response) throws RemoteException {
		int brandId = Integer.parseInt(request.getParameter("brandId"));
		int modelId = Integer.parseInt(request.getParameter("modelId"));
		int year = Integer.parseInt(request.getParameter("year"));
		double price = Double.parseDouble(request.getParameter("price"));
		String contact = request.getParameter("contact");

		getMobileOffersProxy().insertOffer(brandId, modelId, year, price,
				contact);
	}

	private MobileOffersProxy getMobileOffersProxy() {
		if (mobileOffersProxy == null) {
			
			String wsdl_url = getServletConfig().getInitParameter("wsdl_url");
			
			mobileOffersProxy = new MobileOffersProxy(wsdl_url);
		}
		return mobileOffersProxy;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
